package eSn.Core;

import javax.swing.JOptionPane;

public class Box {
	/**
	 * 显示一个标准对话框
	 * @param title 标题
	 * @param message 显示正文
	 * @param bi 图标
	 * @param bb 按钮定义
	 * @param bd 指定默认按钮
	 * @return 所选择的按钮,如果点击取消,返回 Enu.None
	 */
	 public static final Enu MsgBox(String Title,String Message,Enu Icon,Enu Button,Enu defButton){
		 //图标的定义
		 int i;
		 switch (Icon) {
		 	case Error:i = JOptionPane.ERROR_MESSAGE;break;					//错误图
		 	case Information:i = JOptionPane.INFORMATION_MESSAGE;break;		//信息图标
		 	case Warning:i = JOptionPane.WARNING_MESSAGE;break;		 		//警告图标
		 	case Question: i = JOptionPane.QUESTION_MESSAGE;break;			//询问图标
		 	default:i = -1;break;											//无图标
		 }
		 //按钮的定义
		 String[] options;
		 switch (Button) {
		 	case AbortRetryIgnore:options = new String[]{"中止","重试","忽略"};break;
		 	case OKCancel:options = new String[]{ "确定", "取消" };break;
		 	case RetryCancel:options = new String[]{ "重试", "取消" };break;
		 	case YesNo:options = new String[]{ "是", "否" };break;
		 	case YesNoCancel:options = new String[]{ "是", "否" ,"取消"};;break;
		 	default:options = new String[]{ "确定" };break;
		 }		 
		 //默认按钮定义
		 int d ;
		 switch (defButton) {
		 	case DefBtn1:d = 0;break;
		 	case DefBtn2:d = 1;break;
		 	case DefBtn3:d = 2;break;
		 	default:d = -1;break;
		}
		 //返回值
		 int r = 0;		 
		 //默认按钮的定义
		 if (d == -1)  {
			 r = JOptionPane.showOptionDialog(
					 null, 
					 Message, 
					 Title,
					 JOptionPane.DEFAULT_OPTION,
					 i,
					 null,
					 options,
					 null);
		 }else{
			 //处理默认按钮越界问题
			 if ((options.length -1) > d) d = 0;
 			 r = JOptionPane.showOptionDialog(
					 null, 
					 Message, 
					 Title,
					 JOptionPane.DEFAULT_OPTION,
					 i,
					 null,
					 options,
					 options[d]);
		 }
		 
		 switch(r){
		 case 0:
			 switch (Button) {
			 	case AbortRetryIgnore:return Enu.Abort;
			 	case YesNoCancel:return Enu.Yes;
			 	case OKCancel:return Enu.OK;
			 	case RetryCancel:return Enu.Retry;
			 	case YesNo:return Enu.Yes;
			 }
			 break;
		 case 1:
			 switch (Button) {
			 	case AbortRetryIgnore:return Enu.Retry;
			 	case YesNoCancel:return Enu.No;
			 	case OKCancel:return Enu.Cancel;
			 	case RetryCancel:return Enu.Cancel;
			 	case YesNo:return Enu.No;
			 }
			 break;
		 case 2: 
			 switch (Button) {
			 	case AbortRetryIgnore:return Enu.Ignore;
			 	case YesNoCancel:return Enu.Cancel;
			 }
			 break;
		 default:
			 return Enu.None;
		 }
		 return Enu.None;
	 }

	 public static final void MsgBox(Object message){
		  MsgBox("Message Box",String.valueOf(message),Enu.None,Enu.OKOlny,Enu.DefBtn1);
	 }

	 public static final void Msgbox(Object message,Object title){
		  MsgBox(String.valueOf(title),String.valueOf(message),Enu.None,Enu.OKOlny,Enu.DefBtn1);
	 }
	 
	 /**
	  * 标准输入对话框
	  * @param message 提示信息
	  * @param title 窗体标题
	  * @param bi 图标类型
	  * @return 返回用户输入的字符串，如果取消或关闭，则返回 null
	  */
	 public static final Object InputBox(String message,String title,Enu icon){
		 //图标的定义
		 int i;
		 switch (icon) {
		 	case Error:i = JOptionPane.ERROR_MESSAGE;break;					//错误图
		 	case Information:i = JOptionPane.INFORMATION_MESSAGE;break;		//信息图标
		 	case Warning:i = JOptionPane.WARNING_MESSAGE;break;		 		//警告图标
		 	case Question: i = JOptionPane.QUESTION_MESSAGE;break;			//询问图标
		 	default:i = -1;break;											//无图标
		 }
		 return JOptionPane.showInputDialog(null, message, title, i);
 	 }
	 
	 public static final Object InputBox(String message,String title){
		 return InputBox(message, title, Enu.None);
 	 }	 
	 
	 /***
	  * 
	  * @param title
	  * @param message
	  * @param options
	  * @param Default
	  * @return
	  */
	 public static final Object ComboBox(String title,String message,String[] options,String Default){

		 return JOptionPane.showInputDialog(
				 null, 
				 message, 
				 title,
				 JOptionPane.DEFAULT_OPTION,
				 null, 
				 options, 
				 Default);
 
	 }
}
